package org.vectrics.recruiter.struts.tag;

import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.apache.ecs.html.IMG;
import org.apache.ecs.html.Input;
import org.apache.log4j.Category;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Form;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.view.SimpleForm;
import org.vectrics.location.Location;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.struts.admin.SaveLocationAction;
import org.vectrics.struts.VectricsTagSupport;

public class LocationEditTag extends VectricsTagSupport {
	public static final long serialVersionUID = 82334234;

    static private Category log = Category.getInstance(LocationEditTag.class);
    
    
	public String makeStartTag()  {
        log.debug("doStartTag() called");
        Properties formProperties = new Properties();
		Properties invalidProperties = new Properties();
		HttpServletRequest request = (HttpServletRequest)super.pageContext.getRequest();
		String contextPath = request.getContextPath();
		Location location = null;

        //String performLoginParam = request.getParameter(SaveUserAction.PERFORM_LOGIN_PARAM);
        
        
        Table mainTable = new Table();
        Row mainRow = mainTable.addRow();
        Cell mainCell = mainRow.addCell();
        
        log.debug("doRender() - getting http params");
        String id = request.getParameter(SaveLocationAction.ID_PARAM);
        
        
        if (id == null) {
            log.debug("doRender() - 'code' param not given, new location being edited.");
        }
        else {
            log.debug("doRender() - 'code' param given looking for location.");
           	location = CoreServiceLocator.getLocationService().findLocation(new Long(id));
            if (location == null) {
                log.error("doRender() - could not find job param: " + id);
            }
        }

        
        if (invalidProperties.size() <= 0) {
            log.debug("doRender() - creating temporary params for user information.");
            
            if (location != null) {
				formProperties.setProperty(SaveLocationAction.ID_PARAM, location.getId().toString());
				formProperties.setProperty(SaveLocationAction.DESCRIPTION_PARAM, location.getDescription());
            	formProperties.setProperty(SaveLocationAction.NAME_PARAM, location.getName());
            }
        }
        else {
           log.debug("doRender() - request has invalid params, assuming that this form is being shown to fix bad submission.");
        }
        
        
        log.debug("doRender() - creating job edit entry form.");
        SimpleForm form = new SimpleForm(formProperties, invalidProperties);
        form.setMethod(Form.post);
        mainCell.addElement(form.toString());
        form.setAction(contextPath + "/admin/jobs/save.do");
        
        if (location != null) {
            form.addHidden(SaveLocationAction.ID_PARAM, location.getId().toString());
        }
        
        form.showInvalidParams();
        
        Table bigTable = new Table();
        form.addElement(bigTable);
        Row row = bigTable.addRow();
        Cell formLeftCell = row.addCell();
        
        Table leftTable = new Table();
        leftTable.setWidth(500);
        formLeftCell.addElement(leftTable);
        
        IMG clearImage = new IMG();
        clearImage.setSrc(contextPath + "/graphics/cleardot.gif");
        clearImage.setWidth(200);
        clearImage.setHeight(1);
        clearImage.setAlt("");
        Row hdrRow = leftTable.addRow();
        Cell labelHdrCell = hdrRow.addCell();
        Cell valueHdrCell = hdrRow.addCell();
        labelHdrCell.addElement(clearImage);
        
        form.setTable(leftTable);


        form.addBlankRow();
        log.debug("doRender() - adding code to form.");
        Input edit = form.makeEditRow(SaveLocationAction.ID_PARAM, "CompanyLocation Code", true);
        edit.setStyle("width:300px");

		form.addBlankRow();
		log.debug("doRender() - adding name to form.");
		edit = form.makeEditRow(SaveLocationAction.NAME_PARAM, "Name", true);
		edit.setStyle("width:300px");

		form.addBlankRow();
		log.debug("doRender() - adding code to form.");
		edit = form.makeEditRow(SaveLocationAction.DESCRIPTION_PARAM, "Description", true);
		edit.setStyle("width:300px");

        
        form.addSubmitButton("Submit", "Cancel");
            
        log.debug("doRender() - done.");
        return(mainTable.toString());

    }
    
}
